#!/bin/sh

# Slackware build script for atari800

# Written by B. Watson (yalhcru@gmail.com)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

PRGNAM=atari800
VERSION=${VERSION:-3.1.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

# allow bulding without OpenGL, in case someone needs it
if [ "${OPENGL:-yes}" = "yes" ]; then
  GLWITH="with"
else
  GLWITH="without"
fi

GLOPT="--$GLWITH-opengl"

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

cd src

# Include some optional extras. The monitor stuff is nice if you're
# developing code for the Atari, and the extra sound stuff makes for
# a more accurate emulation. The R: device options might let someone
# run an old-school Atari BBS you can telnet to.
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  $GLOPT \
  --with-video=sdl \
  --with-sound=sdl \
  --enable-monitorprofile \
  --enable-monitorbreakpoints \
  --enable-monitortrace \
  --enable-seriosound \
  --enable-riodevice \
  --enable-rnetwork \
  --enable-volonlysound \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --build=$ARCH-slackware-linux

make
make install DOC_DIR=/usr/doc/$PRGNAM-$VERSION MAN_DIR=/usr/man/man1 DESTDIR=$PKG

gzip $PKG/usr/man/man1/$PRGNAM.1
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# 'make install' misses a lot of the docs, let's add them.
cd ../DOC
rm -f directx.txt
cp -a BUGS CREDITS ChangeLog FAQ TODO *.txt $PKG/usr/doc/$PRGNAM-$VERSION
cd ..

# util/ is worth keeping, but mostly for reference (so it goes in /usr/doc)
chmod -x util/*.* util/export
cp -a util $PKG/usr/doc/$PRGNAM-$VERSION

# the act/*.act files are palettes from the dark ages, leave them out.

# nice copyright-friendly icon
mkdir -p $PKG/usr/share/pixmaps
cat data/atari2.png > $PKG/usr/share/pixmaps/$PRGNAM.png

# .desktop file written for this build
mkdir -p $PKG/usr/share/applications
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

mkdir -p $PKG/install
sed "s,@WITH@,$GLWITH," $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
